home *** CD-ROM | disk | FTP | other *** search
- Path: port6.sniff.smallmedia.com!user
- From: mhoward@plainfield.bypass.com (Mark Howard)
- Newsgroups: comp.lang.c
- Subject: Newbie - I'm stuck
- Date: Sat, 27 Jan 1996 18:16:57 -0500
- Organization: TGF Internet Services
- Message-ID: <mhoward-2701961816570001@port6.sniff.smallmedia.com>
- NNTP-Posting-Host: port3.sniff.smallmedia.com
-
- Hi all,
- I'm stuck. The problem was: Write a program that takes a 32 bit int and
- splits it into 8 4-bit values. What I've got seems to work for the first
- 16 bits in num, but then stops recognizing set bits. I've stepped through
- this a bunch of times in the debugger but I'm still clueless!
-
- Thanks, Mark
-
- ---- program that doesn't work -----
- #include <stdio.h>
-
- int main(void)
- {
- char line[20];
- unsigned long num; /* our long int to split */
- int cntr1, cntr2, bitCntr; /* counters */
- char halfbyt[8];
-
- printf("Enter a number: "); /* Get a num */
- fgets(line, sizeof(line), stdin);
- sscanf(line, "%lu", &num);
-
- printf("You Entered (decimal) --> %lu\n", num); /* Display what
- you got*/
- printf("You Entered (hex) --> %#.8lx\n", num); /* Display num as hex */
-
- cntr1 = 0;
- bitCntr = 0;
- while(bitCntr < 32)
- {
- halfbyt[cntr1] = NULL; /* clear each char before setting
- the bits */
- for(cntr2 = 0; cntr2 < 4; cntr2 ++)
- {
- if((num & (1 << bitCntr)) != 0) /* if bit in num
- is setè*/
- /* set corresponding bit in halfbyt[] */
- halfbyt[cntr1] |= (1 << cntr2);
- bitCntr ++;
- }
- cntr1 ++;
- }
-
- printf("Split:\n");
- for(cntr1 = 0; cntr1 < 8; cntr1 ++)
- printf("Half Byte %d --> %#x\n", cntr1, halfbyt[cntr1]);
- }
-
-
- ---------- sample Output ----------------------
- Enter a number: 1234567
- You Entered (decimal) --> 1234567
- You Entered (hex) --> 0x0012d687
- Split:
- Half Byte 0 --> 0x7
- Half Byte 1 --> 0x8
- Half Byte 2 --> 0x6
- Half Byte 3 --> 0xd
- Half Byte 4 --> 0 --- should be 0x2
- Half Byte 5 --> 0 --- should be 0x1
- Half Byte 6 --> 0
- Half Byte 7 --> 0
-